package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	avgLineNum = 34 // 输出的平均组数
)

func main() {
	fp, err := os.Open(os.Getenv("HOME") + "/latency.log")
	if err != nil {
		fmt.Println("open latency log error", err)
	}
	// 统计行数
	bufReader := bufio.NewReader(fp)
	count := 0
	for {
		_, _, err := bufReader.ReadLine()
		if err != nil {
			break
		}
		count++
	}
	// 按组求平均值
	fp.Seek(0, 0)
	bufReader = bufio.NewReader(fp)
	stepLength := count / avgLineNum
	// fmt.Println("step length:", stepLength)
	for i := 0; i < avgLineNum; i++ {
		timeSum := 0
		for j := 0; j < stepLength; j++ {
			line, _, err := bufReader.ReadLine()
			if err != nil {
				break
			}
			lan, err := strconv.Atoi(string(line))
			if err != nil {
				fmt.Println("Atoi error")
			}
			timeSum += lan
		}
		// fmt.Println("time sum:", timeSum)
		fmt.Println(timeSum / stepLength)
	}
}
